<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [广告管理]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Adsense;
use app\admin\model\Ad as M;
use think\facade\Request;
class Ad extends Base
{
    //广告列表
    public function index(){
        if(Request::isAjax()) {
            //分页
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $page = ['list_rows'=> $pageSize, 'page' => $page];

            $key=input('post.key');
            $where = [];
            if (!empty($key)) {
                $where[] = ['title', 'like', '%' . $key . '%'];
            }
            //排序
            $sort_order = input('sort_order') ? input('sort_order') : 'asc';
            $order = input('sort_order')?[input('sort_by')=>$sort_order]:['create_time'=>$sort_order];

            $list = M::getList($where, $page,$order)->toArray();

            return ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }else{
            return view();
        }
    }
    //字段值修改
    public function cf(){
        $data = input('post.');
        $result = M::editSave($data);
        return $result;
    }
    // 添加
    public function add(){
        if(Request::isPost()){
            $data=input('post.');
            $result = M::create($data);
            if ($result) {
                if(isset($data['aid'])){
                    setAttachment('ad','pic', $data['aid'],$result->id);
                }
                return ['code'=>0,'msg'=>'添加成功'];
            } else {
                return ['code'=>1,'msg'=>'添加失败'];
            }

        } else {
            $adsense = Adsense::order('sort asc')->select();
            $view = [
                'adsense' => $adsense,
                'info'      => ''
            ];
            return view('form',$view);
        }
    }
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $result = M::editSave($data);
            if(isset($data['aid'])){
                setAttachment('ad','pic', $data['aid'],$data['id']);
            }
            return $result;
        }else {
            $id = input('param.id');
            if (empty($id)) {
                return ['code' => 1, 'msg' => '缺少必要的参数！'];
            }
            $info = M::where('id', $id)->find();
            $adsense = Adsense::order('sort asc')->select();
            $view = [
                'info' => $info,
                'adsense' => $adsense,
            ];
            return view('form', $view);
        }
    }
    //删除广告
    function del($id) {
        M::destroy($id);
        delAttachment('ad',$id);
        return ['code'=>0,'msg'=>'删除成功!'];
    }

}